package com.zzl.boot.bigevent.controller;

import com.zzl.boot.bigevent.pojo.result;
import com.zzl.boot.bigevent.pojo.user;
import com.zzl.boot.bigevent.service.commentService;
import com.zzl.boot.bigevent.service.userService;
import com.zzl.boot.bigevent.service.warehouseService;
import com.zzl.boot.bigevent.utils.JWTUtils;
import com.zzl.boot.bigevent.utils.MD5Utils;
import com.zzl.boot.bigevent.utils.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Validated//参数校验
@CrossOrigin//允许跨区请求
public class userController {
    @Autowired
    private userService service;
    @Autowired
    private warehouseService wservice;
    @Autowired
    private StringRedisTemplate redisTemplate;
    //注册
    @PostMapping("/register")
    public result regist(@Pattern(regexp = "^\\S{2,16}$") String username,
                         @Pattern(regexp = "^\\S{5,16}$") String password) {
        user user = service.getByUser(username);
        if(user != null) {
            return result.error("用户名已存在");
        }else {
            password = MD5Utils.MD5(password);//对密码进行加密处理
            service.regist(username,password);
            wservice.fristWarehouse(username);
            return result.success();
        }
    }
    //登录
    @PostMapping("/login")
    public result<String> login(@Pattern(regexp = "^\\S{2,16}$") String username,
                                @Pattern(regexp = "^\\S{5,16}$") String password) {
        user user = service.getByUser(username);
        if(user == null) {
            return result.error("账号错误");
        }
        if(MD5Utils.valid(password,user.getPassword())) {
            String token = null;
            token = JWTUtils.getToken(user.getId(), user.getUsername());
            redisTemplate.opsForValue().set(token,token, 2, TimeUnit.DAYS);
            return result.success(token);
        }else {
            return result.error("密码错误");
        }
    }
    //获取用户信息
    @GetMapping("/userInfo")
    public result<user> getUserInfo() {
        Map<String, Object> map = ThreadLocalUtil.get();
        user user = service.getByUser(map.get("username").toString());
        return result.success(user);
    }
    //更新用户信息
    @PutMapping("/update")//添加@Validated校验实体类中的注解
    public result updateUserInfo(@RequestBody @Validated user user) {
        service.updateUserInfo(user);
        return result.success();
    }

    //更新头像   //@URL  判断参数是否符合url地址格式
    @PatchMapping("/updateVat")
    public result updateVat(@RequestParam @URL String vatURl) {
        Map<String, Object> map= ThreadLocalUtil.get();
        service.updateVat((Integer)map.get("id"),vatURl);
        return result.success();
    }
    //更新用户密码
    @PatchMapping("/updatePwd")
    public result updatePwd(@RequestBody Map<String,String> pwds,@RequestHeader("Authorization") String token) {
        Map<String, Object> map= ThreadLocalUtil.get();
        String oldPwd = pwds.get("oldPassword").toString();
        String newPwd = pwds.get("newPassword").toString();
        String cfmPwd = pwds.get("confirmPassword").toString();
        System.out.println(oldPwd);
        System.out.println(newPwd);
        System.out.println(cfmPwd);
        if(!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(cfmPwd)||!StringUtils.hasLength(newPwd)) {
            return result.error("密码不能为空");
        }
        user user = service.getByUser(map.get("username").toString());
        if(!MD5Utils.valid(oldPwd,user.getPassword())) {
            return result.error("原密码错误");
        }else {
            if(newPwd.equals(cfmPwd)) {
                service.updatePwd((Integer)map.get("id"),MD5Utils.MD5(newPwd));
                redisTemplate.opsForValue().getOperations().delete(token);
                return result.success();
            }else {
                return result.error("密码不一致");
            }
        }
    }

    //根据id获取用户信息
    @GetMapping("/detail")
    public result<user> detail( Integer id) {
        System.out.println(id);
        user user =service.getById(id);
        System.out.println(user);
        return result.success(user);
    }
}
